package com.epfresh.supplier.widget;

import android.text.TextUtils;
import android.util.Log;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.supplier.entity.*;
import com.epfresh.supplier.entity.CustomerItem;
import com.epfresh.supplier.entity.SortModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhuli on 2017/8/22.
 */

public abstract  class RequestCustomerListActivity extends BaseActivity {
    public ArrayList<CustomerItem> mContact;
    /**
     * 汉字转换成拼音的类
     */
    private CharacterParser characterParser;
    private ArrayList<SortModel> sourceDateList;
    public int groupSize = 0;
    /**
     * 根据拼音来排列ListView里面的数据类
     */
    private PinyinComparator pinyinComparator;
    public void getCustomerList(String merchantId, String name, String groupId) {

        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_account_customer_list);
        Map<String, String> map = new HashMap<>();
        map.put("merchantId", BaseApplication.getInstance().getMerchantId());
        map.put("name", TextUtils.isEmpty(name) ? "":name);
        map.put("groupId", TextUtils.isEmpty(groupId) ? null:groupId);
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_account_customer_list, onRequestListener);
    }

    private OnRequestListener<CustomerInfo> onRequestListener = new OnRequestListener<CustomerInfo>() {
        @Override
        public CustomerInfo jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, new TypeToken<CustomerInfo>() {
            }.getType());
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<CustomerInfo> re, Object tag) {
            hideProgressDialog();
            groupSize = re.getResponseElement().getGroupSize();
            Log.e("groupSize Start:",""+groupSize);
            characterParser = CharacterParser.getInstance();
            pinyinComparator = new PinyinComparator();

            CustomerInfo customerInfo = re.getResponseElement();
            //将list赋给
            mContact = customerInfo.getCustomers();
            initContactData(mContact);
            updateUI(sourceDateList);
        }
    };

    protected abstract void updateUI(ArrayList<SortModel> mContact);

    private void initContactData(ArrayList<CustomerItem> mContact) {
        if (null == mContact || 0 == mContact.size()) {
            sourceDateList = new ArrayList<>();
            return;
        }
        sourceDateList = filledData(mContact);
        // 根据a-z进行排序源数据
        Collections.sort(sourceDateList, pinyinComparator);
    }

    /**
     * 为List填充数据
     *
     * @param data
     * @return
     */
    private ArrayList<SortModel> filledData(List<CustomerItem> data) {
        ArrayList<SortModel> mSortList = new ArrayList<SortModel>();
        long time = new Date().getTime();
        Log.e("request_____",""+time);
        for (int i = 0; i < data.size(); i++) {
            SortModel sortModel = new SortModel();
            sortModel.setCustomerItem(data.get(i));
            // 汉字转换成拼音
            if(!TextUtils.isEmpty(data.get(i).getName())) {
                String pinyin = characterParser.getSelling(data.get(i).getName());
                String sortString = pinyin.substring(0, 1).toUpperCase();

                // 正则表达式，判断首字母是否是英文字母
                if (sortString.matches("[A-Z]")) {
                    sortModel.setSortLetters(sortString.toUpperCase());
                } else {
                    sortModel.setSortLetters("#");
                }
            }else {
                sortModel.setSortLetters("#");
            }
            mSortList.add(sortModel);
        }
        Log.e("request__end___",""+new Date().getTime());
        Log.e("request__end___",""+(new Date().getTime()-time));

        return mSortList;

    }

}
